
import os
import shutil
import sys
import time
import math
import json
import zipfile
from subprocess import run
import subprocess
from PIL import Image
# import thread
# import thread6

import FileUtils
import ArrayUtils

curPath = os.getcwd()
print(curPath)

# projectPath = os.path.abspath(os.path.join(os.path.dirname(curPath), os.pardir))
# print(projectPath)

import platform
sysstr = platform.system()
print(platform)

pngList = []

size = 0
compressSize = 0

# from subprocess import run
#             run("pngquant --quality=65-80 --force --ext .png " + filepath",shell=True)

#忽略图片列表 关键字
ignorePngList = ["role_1.png","role_1_down.png","role_1_up.png"]



#阻塞
def CompressedPngSystem(command):
	os.system(command)


#window,多线程,比较快
def CompressedPngSubprocess(command):
	subprocess.Popen(command, shell=True)



def CompressedPng():
	def callback(filepath,filename):
		if ArrayUtils.isInArray(ignorePngList,filename):
			print("忽略压缩图片:",filename)
			return
		pngList.append(filepath)
		pass
	#D:/projectXH\creator2d\BanZhuanH5\client\assets\res\BattleScene\BattleSpineDir_all
	FileUtils.diguiDirWithTail(os.path.join(curPath,"../assets/res"),callback,".png")


	fileLen = len(pngList)
	# 一个一个压缩
	# for filepath in pngList:
	for index in range(fileLen):
		filepath = pngList[index]
		global size
		# size = size + FileUtils.getFileSize(filepath)
		if sysstr == "Windows":
			command = "pngquant --quality=1-10 --force --ext .png " + filepath
			os.system(command)
		else:
			command = "nohup pngquant --quality=65-80 --force --ext .png " + filepath + " >/dev/null 2>&1 &"
			os.system(command)
		print("index,fileLen = %d / %d"%(index,fileLen))


	pass




#arg[1]:渠道名:proj.ios_mac_appstore
if __name__ == "__main__":
	

	CompressedPng()


	pass



